const path = require('path')
const chalk = require('chalk')
const webpack = require('webpack')

// FILE

// PLUGINS
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
/* 增强webpack-dev-server */
const ProgressBarPlugin = require('progress-bar-webpack-plugin')
// const DashboardPlugin = require('webpack-dashboard/plugin')
// const Dashboard = require('webpack-dashboard')
// const dashboard = new Dashboard()

module.exports = {
  entry: path.resolve(__dirname, '../src/index.js'),
  output: {
    path: path.resolve(__dirname, '../dist'),
    filename: '[name].build.[hash:8].js',
    publicPath: '/',
    chunkFilename: 'chunk.[name].js'
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        include: path.resolve(__dirname, '../src'),
        use: [
          {
            loader: 'babel-loader'
          }
        ]
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        include: path.resolve(__dirname, '../src'),
        use: [
          {
            loader: 'eslint-loader',
            options: {
              fix: true,
              enforce: 'pre'
            }
          }
        ]
      },
      {
        test: /\.(css|less)$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              // modules: true
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: () => [
                require('autoprefixer')([
                  '>1%',
                  'last 4 versions',
                  'Firefox ESR',
                  'not ie < 9' // React doesn't support IE8 anyway
                ])
              ]
            }
          },
          {
            loader: 'less-loader'
          }
        ]
      },
      {
        test: /\.(png|jpg|gif)/,
        use: {
          loader: 'file-loader',
          options: {}
        }
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.css', '.less'],
    alias: {
      '@util': path.resolve(__dirname, '../src/util'),
      '@api': path.resolve(__dirname, '../src/api'),
      '@assets': path.resolve(__dirname, '../src/assets')
    }
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, '../public/index.html'),
      filename: 'index.html',
      minify: {
        removeComments: true, // 移除HTML中的注释
        collapseWhitespace: true, // 删除空白符与换行符
        minifyCSS: true // 压缩内联css
      }
    }),

    new MiniCssExtractPlugin({
      filename: 'zero.[hash:8].css'
    }),
    new ProgressBarPlugin({
      format:
        '  build [:bar] ' +
        chalk.green.bold(':percent') +
        ' (:elapsed seconds)',
      clear: false
    })
    // new webpack.DllPlugin({
    //   manifest: require('../public/vendor/manifest.json'), //指定manifest.json
    //   context: __dirname
    // })
    // new DashboardPlugin(dashboard.setData) //暂时不用
  ]
}
